<?php
/**
 * User: yyw
 * Date: 2017/6/26
 * Time: 17:07
 * Progress:0%
 */

namespace logic\admin\shop;


use library\order\format;
use model\ShopOrder;
use model\ShopOrderGoods;
use model\ShopOrderItem;
use think\Db;

class LShopOrder
{
    public function logic_index()
    {
        $model = new ShopOrder();
        $cal['count'] = $model->count('id');
        $cal['sum'] = $model->sum('order_amount');
        return $cal;

    }

    public function index_data($param)
    {
        $model = new ShopOrder();
        $where = array();
        $status = $param['status'];
        if ($status > -1) {
            $where['s.flag_pay'] = $status;
        }

        $flag_delivery = $param['flag_delivery'];
        if ($flag_delivery > -1) {
            $where['s.flag_delivery'] = $flag_delivery;
        }

        $flag_status = $param['flag_status'];
        if ($flag_status > -1) {
            $where['s.flag_status'] = $flag_status;
        }
        $keywords = $param['search'];
        if (!empty($keywords)) {
            $where['s.order_sn|s.o_name|s.o_mobile|a.name '] = array('like', "%{$keywords}%"); //查询条件
        }

        if (!empty($param['begindate'])) {
            $begin_date = strtotime($param['begindate']);
        } else {
            $begin_date = '';
        }
        if (!empty($param['enddate'])) {
            $end_date = strtotime('+1 day', strtotime($param['enddate']));
        } else {
            $end_date = '';
        }

        if (!empty($begin_date) && !empty($end_date)) {
            $where['s.order_time'] = array('between', "{$begin_date},{$end_date}");
        } else if (!empty($begin_date) && empty($end_date)) {
            $where['s.order_time'] = array('egt', $begin_date);
        } else if (empty($begin_date) && !empty($end_date)) {
            $where['s.order_time'] = array('elt', $end_date);
        }

        $total = $model->alias('s')
            ->join('account a', 'a.id=s.delivery_account_id', 'left')
            ->where($where)
            ->count(); //记录总数

        $page_size = $param['limit']; //获取分页数
        $start = $param['offset']; //获取开始的记录数
        //获取排序字段------------------------------------------------------------开始

        $sort_name = $param['sort'];
        $sort_desc = $param['order'];

        if (!empty($sort_name)) {
            $sort[$sort_name] = $sort_desc;
        } else {
            $sort['s.order_time'] = 'desc'; //默认排序
        }
        //获取排序字段------------------------------------------------------------结束

        $rows = $model->alias('s')
            ->join('account a', 'a.id=s.delivery_account_id', 'left')
            ->where($where)
            ->field("s.*,a.name as dname,(select GROUP_CONCAT(concat_ws('@@@',g.id,kf.filename,g.name,og.quantity,og.amount,og.price,soi.order_sn) SEPARATOR '###') from ks_shop_order_item soi INNER JOIN ks_shop_order_goods og ON og.order_item_id=soi.id  INNER JOIN ks_shop_goods g ON g.id=og.goods_id LEFT JOIN ks_file kf on kf.id=g.cover_image where soi.order_id=s.id ) as goods")
            ->limit($start, $page_size)
            ->order($sort)
            ->select();

        $array = array(
            'total' => $total,
            'rows' => $rows
        );

        return $array;
    }



    public function delivery_data($param)
    {
        $model = new ShopOrder();
        $where = array();

        $flag_delivery = $param['flag_delivery'];
        if ($flag_delivery > -1) {
            $where['s.flag_delivery'] = $flag_delivery;
        }

        $flag_status = $param['flag_status'];
        if ($flag_status > -1) {
            $where['s.flag_status'] = ["in",$flag_status];
        }
        $keywords = $param['search'];
        if (!empty($keywords)) {
            $where['s.order_sn|s.o_name|s.o_mobile|a.name '] = array('like', "%{$keywords}%"); //查询条件
        }
        if (!empty($param['order_time'])) {
            $where["from_unixtime(s.order_time, '%Y-%m-%d')"]=$param['order_time'];
        }
        if (!empty($param['da_id'])) {
            $where["delivery_account_id"]=$param['da_id'];
        }

        $total = $model->alias('s')
            ->join('account a', 'a.id=s.delivery_account_id', 'left')
            ->where($where)
            ->count(); //记录总数

        $page_size = $param['limit']; //获取分页数
        $start = $param['offset']; //获取开始的记录数
        //获取排序字段------------------------------------------------------------开始

        $sort_name = $param['sort'];
        $sort_desc = $param['order'];

        if (!empty($sort_name)) {
            $sort[$sort_name] = $sort_desc;
        } else {
            $sort['s.order_time'] = 'desc'; //默认排序
        }
        //获取排序字段------------------------------------------------------------结束

        $rows = $model->alias('s')
            ->join('account a', 'a.id=s.delivery_account_id', 'left')
            ->where($where)
            ->field("s.*,a.name as dname")
            ->limit($start, $page_size)
            ->order($sort)
            ->select();

        $array = array(
            'total' => $total,
            'rows' => $rows
        );

        return $array;
    }


//结算汇总
    public function summary_data($param)
    {
        $model = new ShopOrder();
        $where = array();

        $flag_delivery = $param['flag_delivery'];
        if ($flag_delivery > -1) {
            $where['s.flag_delivery'] = $flag_delivery;
        }

        $flag_status = $param['flag_status'];
        if ($flag_status > -1) {
            $where['s.flag_status'] = ["in",$flag_status] ;
        }
        $keywords = $param['search'];
        if (!empty($keywords)) {
            $where['a.name '] = array('like', "%{$keywords}%"); //查询条件
        }

        if (!empty($param['begindate'])) {
            $begin_date = strtotime($param['begindate']);
        } else {
            $begin_date = '';
        }
        if (!empty($param['enddate'])) {
            $end_date = strtotime('+1 day', strtotime($param['enddate']));
        } else {
            $end_date = '';
        }

        if (!empty($begin_date) && !empty($end_date)) {
            $where['s.order_time'] = array('between', "{$begin_date},{$end_date}");
        } else if (!empty($begin_date) && empty($end_date)) {
            $where['s.order_time'] = array('egt', $begin_date);
        } else if (empty($begin_date) && !empty($end_date)) {
            $where['s.order_time'] = array('elt', $end_date);
        }

        $subQuery  = $model->alias('s')->join('account a', 'a.id=s.delivery_account_id', 'left')
            ->where($where)
            ->field("SUM(s.order_amount) as amount,a.name,from_unixtime(s.order_time, '%Y-%m-%d') as order_time,s.flag_status,s.flag_delivery,s.delivery_account_id as da_id")
            ->group("from_unixtime(s.order_time, '%Y-%m-%d'),a.name,s.flag_status,s.flag_delivery,s.delivery_account_id")->buildSql();

        $total=Db::table($subQuery.' s')->count();

        $page_size = $param['limit']; //获取分页数
        $start = $param['offset']; //获取开始的记录数
        //获取排序字段------------------------------------------------------------开始

        $sort_name = $param['sort'];
        $sort_desc = $param['order'];

        if (!empty($sort_name)) {
            $sort[$sort_name] = $sort_desc;
        } else {
            $sort['s.order_time'] = 'desc'; //默认排序
        }
        //获取排序字段------------------------------------------------------------结束

        $rows=Db::table($subQuery.' s')
            ->limit($start, $page_size)
            ->order($sort)
            ->select();
        $list = [];
        foreach ($rows as $row) {
            $row['status_text'] = format::order_status($row['flag_status'], 1);
            array_push($list, $row);
        }
        $array = array(
            'total' => $total,
            'rows' => $list
        );

        return $array;
    }

    public function detail_store_item($id)
    {
        $model = new ShopOrderItem();
        $data = $model->alias('s')
            ->join('shop_store ss', 'ss.id=s.store_id')
            ->where('s.order_id', $id)
            ->field('ss.name,s.order_sn as item_order_sn,s.id as item_order_id')
            ->select();
        return $data;
    }

    public function detail_data($id)
    {
        $model = new ShopOrderItem();
        $data = $model->alias('s')
            ->join('shop_order_goods sog', 'sog.order_item_id=s.id')
            ->join('shop_goods sg', 'sg.id=sog.goods_id')
            ->join('file f', 'f.id=sg.cover_image', 'left')
            ->field('sg.*,sog.*,f.filename')
            ->where(['s.id' => $id])
            ->select();

        return $data;
    }

    public function order_data($id)
    {
        $model = new ShopOrder();
        $data = $model->alias('s')
            ->join('account a', 'a.id = s.account_id', 'left')
            ->join('account_payment ap', 'ap.id = s.payment_id', 'left')
            ->join('account ac', 'ac.id=s.delivery_account_id', 'left')
            ->field('s.*,a.name as account_name,ap.pay_type,ac.name as delivery_name')
            ->where(['s.id' => $id])
            ->find();
        return $data;
    }

    //订单状态
    public function order_status()
    {
        $data = format::get_order_status();
        return $data;
    }

    public function set_flag_delivery_pay($type, $ids)
    {
        Db::startTrans();
        $a = Db::name('shop_order')->where('id', 'in', $ids)->setField('flag_delivery_pay', $type);
        $b = Db::name('shop_order_item')->where('order_id', 'in', $ids)->setField('flag_delivery_pay', $type);
        if ($a && $b) {
            Db::commit();
            return ['flag' => true, 'msg' => '结算成功'];
        } else {
            Db::rollback();
            return ['flag' => false, 'msg' => '处理失败'];
        }
    }
}